package com.banyuan.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author 陶继发
 * @version 1.8
 */
@SuppressWarnings("all")
public class JDBCUtil {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    static{
        InputStream input = JDBCUtil.class.getClassLoader().getResourceAsStream("mysql.properties");
        Properties properties = new Properties();
        try {
            properties.load(input);
           url= properties.getProperty("url");
           user = properties.getProperty("user");
           password = properties.getProperty("password");
           driver = properties.getProperty("driver");
        } catch (IOException e) {
            e.printStackTrace();
        }
        try{
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection(url,user,password);
        return connection;
    }

    public static  void executeUpdate(Connection connection ,String updateSql,Object...objects){
        PreparedStatement  pstm = null;
        try {
             pstm = connection.prepareStatement(updateSql);
            for (int i = 0; i < objects.length; i++) {
                pstm.setObject(i+1,objects[i]);
            }
            int i = pstm.executeUpdate();
            System.out.println(i>0?"更新成功":"更新失败");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(pstm);
        }
    }

    public static void executeUpdate(String updateSql,Object...objects){
        Connection connection = null;
        PreparedStatement pstm = null;
        try {
            connection = getConnection();
             pstm = connection.prepareStatement(updateSql);
            for (int i = 0; i < objects.length; i++) {
                pstm.setObject(i+1,objects[i]);
            }
            int i = pstm.executeUpdate();
            System.out.println(i>0?"更新成功":"更新失败");

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(pstm);
            close(connection);
        }
    }


    public static  ResultSet  executeQuery(Connection connection,String querySql,Object...objects){
        PreparedStatement pstm = null;
        ResultSet re = null;
        try {
             pstm = connection.prepareStatement(querySql);
            for (int i = 0; i < objects.length; i++) {
                pstm.setObject(i+1,objects[i]);
            }
             re = pstm.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return re;
    }

    public static ResultSet executeQuery(String querySql,Object...objects){
        Connection connection = null;
        PreparedStatement pstm = null;
        ResultSet re = null;
        try {
            connection=getConnection();
             pstm = connection.prepareStatement(querySql);
            for (int i = 0; i < objects.length; i++) {
                pstm.setObject(i+1,objects[i]);
            }
             re = pstm.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return re;
    }


    public static void close(AutoCloseable ac){
        if(ac!=null){
            try {
                ac.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
